package net.app.zoneland.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.app.zoneland.domain.SysRole;
import net.app.zoneland.domain.SysRoleMenu;
import net.app.zoneland.dto.MenuDTO;
import net.app.zoneland.platform.mapper.SysRoleMenuMapper;
import net.app.zoneland.platform.service.ISysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author LENOVO
 * @CreateTime 2020/12/26 13:08
 * @Version: 1.0
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

    @Override
    public List<MenuDTO> findByRoleId(Integer roleId) {
        return this.baseMapper.findByRoleId(roleId);
    }

    @Override
    @Transactional
    public void updateInfo(Integer roleId, String ids) {
        LambdaQueryWrapper<SysRoleMenu> query = new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId,roleId);
        // 1.先清空原本的权限
        this.baseMapper.delete(query);

        List<SysRoleMenu> list=new ArrayList<>();
        if(ids.indexOf(",")>-1){
            String[] mens = ids.split(",");
            for (String menId : mens) {
                SysRoleMenu men=new SysRoleMenu();
                men.setRoleId(roleId);
                Integer mId = Integer.valueOf(menId);
                men.setMenuId(mId);
                list.add(men);
            }
        }else{
            SysRoleMenu men=new SysRoleMenu();
            men.setRoleId(roleId);
            Integer mId = Integer.valueOf(ids);
            men.setMenuId(mId);
            list.add(men);
        }
        // 2.插入新权限
        this.baseMapper.insertAuthBatch(list);
    }

}
